import socket


def main():
    """文件下载器-客户端"""
    # 创建套接字
    client_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 连接服务器
    client_soc.connect(("127.0.0.1", 1315))

    # 发送文件名
    file_name = input('请输入文件名:')
    client_soc.send(file_name.encode('utf-8'))

    # 接收文件内容
    recv_data = client_soc.recv(1024*1024)

    if recv_data:
        # 保存内容到文件里
        with open('[新]' + file_name, 'wb') as file:
            file.write(recv_data)
        #     print(file.closed)
        # print(file.closed)


    # 关闭套接字
    client_soc.close()


if __name__ == '__main__':
    main()
